﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using COFE.IO;
using COFE.Core;
using COFE.IO.FSIEX.Utils;
using System.IO;
using System.Drawing;
using COFE.Core.Utils;
using NUnit.Framework;
using COFE.Core.Entry;

namespace COFE.Core.Lister
{
    public class ExIconPropertyProvider : ISecondaryPropertyProvider<IFileSystemInfo>
    {
        #region Constructors

        public ExIconPropertyProvider()
        {
        }

        #endregion

        #region Methods

        public object[] GetSupportedProperty(IFileSystemInfo sourceObj)
        {
            if (sourceObj is IFileSystemInfo)
            {
                var propProvider = (sourceObj as IInternalFileSystemInfoExA).PrimaryPropertyProvider;
                if (propProvider is COFE.IO.FSIEX.FileSystemExPropertyProvider)
                    return new object[] {
                        IconProperties.SmallIcon,
                        IconProperties.LargeIcon,
                        IconProperties.ExtraLargeIcon,
                        IconProperties.JumboIcon
                    };

            }
            return new object[] { };
        }

        public bool IsPropertyReadOnly(IFileSystemInfo sourceObj, object property)
        {
            return true;
        }

        public PropertyPair GetProperty(IFileSystemInfo sourceObj, object property)
        {
            if (sourceObj is IFileSystemInfo)
            {
                var sourceEntry = sourceObj as IInternalFileSystemInfoExA;

                Func<IInternalFileSystemInfoExA, IconSize, Bitmap> getIcon = (entry, iconsize) =>
                {
                    if (entry.IsPropertySupported(InternalProperties.EmbeddedEntry))
                    {
                        var ppair = entry.GetProperty(InternalProperties.EmbeddedEntry);
                        if (ppair.HasValue)
                        {
                            FileSystemInfoEx embEntry = ppair.Value as FileSystemInfoEx;
                            if (embEntry != null)
                                return _exiconv.GetIcon(embEntry, IconSize.small, embEntry is DirectoryInfoEx, true);
                        }
                    }
                    return _exiconv.GetIcon(entry.TempPath, IconSize.small, entry.IsFolder);
                };

                switch ((int)property)
                {
                    case (int)IconProperties.SmallIcon:
                        return new PropertyPair(property, getIcon(sourceEntry, IconSize.small).EnsureBitmapSize(IconProperties.SmallIcon));
                    case (int)IconProperties.LargeIcon:
                        return new PropertyPair(property, getIcon(sourceEntry, IconSize.large).EnsureBitmapSize(IconProperties.LargeIcon));
                    case (int)IconProperties.ExtraLargeIcon:
                        return new PropertyPair(property, getIcon(sourceEntry, IconSize.extraLarge).EnsureBitmapSize(IconProperties.ExtraLargeIcon));
                    case (int)IconProperties.JumboIcon:
                        return new PropertyPair(property, getIcon(sourceEntry, IconSize.jumbo).EnsureBitmapSize(IconProperties.JumboIcon));
                }

            }

            throw new NotSupportedException();
        }

        public void SetProperty(IFileSystemInfo sourceObj, object property, object value)
        {
            throw new NotImplementedException();
        }

       
        
        #endregion

        #region Data

        private static ExIconExtractor _exiconv = new ExIconExtractor();

        #endregion



    }
}
